<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('IN_ADMIN', true);
include("../includes/common.php");
if ($admin_islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");

$act = _get('act');
switch ($act) {
    case 'add':
        
        $name      = _post('name');
        $catename  = _post('catename');
        $introduce = _post('introduce');
        $tui       = _post('tui');
        $cover     = _post('cover');
        if (empty($name)) {
            add_action_log($conf['admin_user'], '添加文章失败：标题不能为空', 0);
            exit("<script>alert('标题不能为空');history.go(-1)</script>");
        }
        if (empty($catename)) {
            add_action_log($conf['admin_user'], '添加文章失败：分类不能为空', 0);
            exit("<script>alert('分类不能为空');history.go(-1)</script>");
        }
        
        
        $count = $DB->count('article', array('name' => $name));
        if ($count > 0) {
            add_action_log($conf['admin_user'], '添加文章失败：标题已存在', 0);
            exit("<script>alert('标题已存在！');history.go(-1)</script>");
        }
        
        $result = $DB->insert('article', array(
            'name'       => $name,
            'catename'   => $catename,
            'introduce'  => $introduce,
            'hits_total' => 0,
            'tui'        => $tui,
            'cover'      => $cover,
            'time'       => date("Y-m-d H:i:s"),
        ));
        
        if ($result) {
            add_action_log($conf['admin_user'], '添加文章：' . $name, 1);
            exit("<script>alert('添加成功！');location='./article.php';</script>");
        } else {
            add_action_log($conf['admin_user'], '添加文章失败：' . $name, 0);
            exit("<script>alert('添加失败！');history.go(-1)</script>");
        }
        break;
        
    case 'edit':
        
        $id        = _post('id');
        $name      = _post('name');
        $catename  = _post('catename');
        $introduce = _post('introduce');
        $tui       = _post('tui');
        $cover     = _post('cover');
        $time      = date("Y-m-d H:i:s");
        if (empty($id)) {
            add_action_log($conf['admin_user'], '编辑文章失败：ID不能为空', 0);
            exit("<script>alert('ID不能为空');history.go(-1)</script>");
        }
        if (empty($name)) {
            add_action_log($conf['admin_user'], '编辑文章失败(ID: ' . $id . ')：标题不能为空', 0);
            exit("<script>alert('标题不能为空');history.go(-1)</script>");
        }
        if (empty($catename)) {
            add_action_log($conf['admin_user'], '编辑文章失败(ID: ' . $id . ')：分类不能为空', 0);
            exit("<script>alert('分类不能为空');history.go(-1)</script>");
        }
        
        
        $count = $DB->count('article', "`name`='$name' AND `id`<>$id");
        if ($count > 0) {
            add_action_log($conf['admin_user'], '编辑文章失败(ID: ' . $id . ')：标题已存在', 0);
            exit("<script>alert('标题已存在！');history.go(-1)</script>");
        }
        
        $result = $DB->update('article', array(
            'name'      => $name,
            'catename'  => $catename,
            'introduce' => $introduce,
            'tui'       => $tui,
            'cover'     => $cover,
            'time'      => date("Y-m-d H:i:s"),
        ), array('id' => $id));
        
        if ($result) {
            add_action_log($conf['admin_user'], '编辑文章(ID: ' . $id . ')：' . $name, 1);
            exit("<script>alert('修改成功！');location='./article.php';</script>");
        } else {
            add_action_log($conf['admin_user'], '编辑文章失败(ID: ' . $id . ')：' . $name, 0);
            exit("<script>alert('修改失败！');history.go(-1)</script>");
        }
        break;
        
    case 'del':
        
        @header('Content-Type: application/json; charset=UTF-8');
        $id = _post('id');
        if (!$id) {
            exit('{"code":-1,"msg":"ID不能为空！"}');
        }
        
        
        $article_info = $DB->find('article', 'name', array('id' => $id));
        $article_name = $article_info ? $article_info['name'] : '未知文章';
        
        $result = $DB->delete('article', array('id' => $id));
        if ($result) {
            add_action_log($conf['admin_user'], '删除文章(ID: ' . $id . ')：' . $article_name, 1);
            exit('{"code":0,"msg":"删除成功！"}');
        } else {
            add_action_log($conf['admin_user'], '删除文章失败(ID: ' . $id . ')：' . $article_name, 0);
            exit('{"code":-1,"msg":"删除失败！"}');
        }
        break;
        
    default:
        @header('Content-Type: application/json; charset=UTF-8');
        exit('{"code":-4,"msg":"No Act"}');
        break;
}